# This is is a simple Makefile used to build my PYTHIA8 program.

################################################################################
# VARIABLES: Definition of the relevant variables from the configuration script.
################################################################################

# Set the shell.
SHELL=/usr/bin/env bash

# Include the configuration.
-include Makefile.inc

# Check distribution (use local version first, then installed version).
ifneq ("$(wildcard ../lib/libpythia8.*)","")
  PREFIX_LIB=../lib
  PREFIX_INCLUDE=../include
endif
CXX_COMMON:=$(OBJ_COMMON) -I$(PREFIX_INCLUDE) $(CXX_COMMON) $(GZIP_LIB)
CXX_COMMON+= -L$(PREFIX_LIB) -Wl,-rpath,$(PREFIX_LIB) -lpythia8 -ldl
PYTHIA=$(PREFIX_LIB)/libpythia8$(LIB_SUFFIX)

# Define RIVET options and fix C++ version, rpath, missing HDF5.
ifeq ($(RIVET_USE),true)
  COMMA=,
  RIVET_VERSION=$(shell $(RIVET_BIN)$(RIVET_CONFIG) --version)
  RIVET_LPATH=$(filter -L%,$(shell $(RIVET_BIN)$(RIVET_CONFIG) --ldflags))
  RIVET_OPTS=$(subst -L,-Wl$(COMMA)-rpath$(COMMA),$(RIVET_LPATH))
  RIVET_OPTS+= $(shell $(RIVET_BIN)$(RIVET_CONFIG) --cppflags --libs)
  RIVET_CSTD=c++14
  ifeq ("4.0.0","$(word 1, $(sort 4.0.0 $(RIVET_VERSION)))")
    RIVET_CSTD=c++17
    RIVET_LDIR=$(shell $(RIVET_BIN)$(RIVET_CONFIG) --libdir)
    RIVET_HDF5=$(shell nm $(RIVET_LDIR)/libRivet$(LIB_SUFFIX) | grep H5open)
    ifneq ($(strip $(RIVET_HDF5)),)
      RIVET_OPTS+= -lhdf5
    endif
  endif
  RIVET_OPTS+= $(RIVET_FLAGS) $(CXX_COMMON:c++11=$(RIVET_CSTD)) $(CXX_DTAGS)
  RIVET_OPTS+= -DRIVET
endif

# Define additional dependency options.
EVTGEN_OPTS=$(EVTGEN_INCLUDE) $(EVTGEN_LIB) -DEVTGEN_PYTHIA -DEVTGEN_EXTERNAL
HEPMC2_OPTS=$(HEPMC2_INCLUDE) $(HEPMC2_LIB) -DHEPMC2
HEPMC3_OPTS=$(HEPMC3_INCLUDE) $(HEPMC3_LIB) -DHEPMC3
HDF5_OPTS=$(HDF5_INCLUDE) $(HIGHFIVE_INCLUDE) $(MPICH_INCLUDE)
HDF5_OPTS+= $(HDF5_LIB) -DHDF5
FASTJET3_OPTS=$(FASTJET3_INCLUDE) $(FASTJET3_LIB)
ifeq ($(ROOT_USE),true)
  ROOT_OPTS=$(ROOT_LIB) $(shell $(ROOT_CONFIG) --cflags --glibs) -DPY8ROOT
endif

################################################################################
# RULES: Definition of the rules used to build the PYTHIA examples.
################################################################################

# Rules without physical targets (secondary expansion for specific rules).
.SECONDEXPANSION:
.PHONY: all clean

# All targets.
all: xMyPythia8.x
	$(info xMyPythia8.x generated.)

# PYTHIA library.
$(PYTHIA):
	$(error Error: PYTHIA must be built, please run "make"\
                   in the top PYTHIA directory)

# PYTHIA 8 with ROOT.
xMyPythia8.x: $(PYTHIA) MyPythia8.cpp
ifeq ($(ROOT_USE),true)
	$(CXX) MyPythia8.cpp -o $@ $(CXX_COMMON) $(ROOT_OPTS)
else
	$(error Error: $@ requires ROOT)
endif

# Clean.
clean:
	rm -f *.out;
	rm -f *.dat;
	rm -f *.log;
	rm -f *.x;
	rm -f *.o;
	rm -f *.root;